package com.whut.monitor.controller;

import com.whut.monitor.vo.Tree;
import com.whut.monitor.model.EquipmentEntity;
import com.whut.monitor.service.IEquipmentService;
import com.whut.monitor.vo.ComboVo;
import com.whut.monitor.vo.EquipmentVo;
import com.whut.monitor.vo.Grid;
import com.whut.monitor.vo.PageFilter;
import com.whut.monitor.vo.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author CFlower
 * 设备管理接口
 */
@Controller
@RequestMapping("/equipment")
public class EquipmentController {

	@Autowired
	private IEquipmentService equipmentService;

	@RequestMapping("/manager")
	public String manager() {
		return "/mintor/basicInformation/equipment";
	}

	@RequestMapping("/dataGrid")
	@ResponseBody
	public Grid dataGrid(EquipmentVo equipment, PageFilter pageFilter) {
		Grid grid = new Grid();
		grid.setRows(equipmentService.dataGrid(equipment, pageFilter));
		grid.setTotal(equipmentService.count(equipment, pageFilter));
		return grid;
	}

	@RequestMapping("/add")
	@ResponseBody
	public ResponseMsg add(EquipmentVo equipmentVo) {
		ResponseMsg j = new ResponseMsg();
		try {
			equipmentService.add(equipmentVo);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/edit")
	@ResponseBody
	public ResponseMsg edit(EquipmentVo equipmentVo) {
		ResponseMsg j = new ResponseMsg();
		try {
			equipmentService.edit(equipmentVo);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public ResponseMsg delete(Integer id) {
		ResponseMsg j = new ResponseMsg();
		try {
			equipmentService.delete(id);
			j.setMsg("删除成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/tree")
	@ResponseBody
	public List<Tree> tree() {
		return equipmentService.tree();
	}

	@RequestMapping("/getAllEquip")
	@ResponseBody
	public List<ComboVo> getAllEquip() {
		List<ComboVo> list=new ArrayList<ComboVo>();
		List<EquipmentEntity> entity=equipmentService.getAllEquip();
		for (EquipmentEntity equipmentEntity : entity) {
			ComboVo comboVo=new ComboVo();
			comboVo.setValue(equipmentEntity.getId());
			comboVo.setText(equipmentEntity.getEquipmentName());
			comboVo.setNum(equipmentEntity.getEquipmentNum());
			list.add(comboVo);
		}
		return list;
	}
}

